﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
namespace QuanLyThuVien
{
    public partial class frmNhapSach : Form
    {
        TheLoaiSachBUS theLoaiBUS = new TheLoaiSachBUS();
        SachBUS sachBUS = new SachBUS();
        DauSachBUS dausachBUS = new DauSachBUS();
        CuonSachBUS cuonsachBUS = new CuonSachBUS();
        PhieuNhapSachBUS phieuNhapBUS = new PhieuNhapSachBUS();
        CTPhieuNhapSachBUS cTPhieuNhapBUS = new CTPhieuNhapSachBUS();
        TacGiaBUS tacGiaBUS = new TacGiaBUS();

        private frmMain mainForm = null;

        public frmNhapSach(Form callingForm)
        {
            mainForm = callingForm as frmMain;
            InitializeComponent();
        }
        private void frmNhapSach_Load(object sender, EventArgs e)
        {
            foreach (string s in theLoaiBUS.GetListTheLoaiSach())
            {
                comboBox_theLoai.Items.Add(s);
            }
            comboBox_tacGia.DataSource = tacGiaBUS.GetListTenTacGia();
            comboBox_NXB.DataSource = sachBUS.GetListTenNXB();
            comboBox_tacGia.Text = "";
            comboBox_NXB.Text = "";
            listBox_tacGia.Items.Clear();
            tb_soLuong.Text = "1";
        }
        private void button_luu_Click(object sender, EventArgs e)
        {
            int soLuong = 1;
            if (InputFormat.formatString(tb_soLuong.Text).Length != 0)
                soLuong = int.Parse(tb_soLuong.Text);
            if (InputFormat.formatString(tb_namXB.Text).Length == 0 || comboBox_NXB.Text.Length == 0
                || listBox_tacGia.Items.Count == 0 || InputFormat.formatString(tb_tenSach.Text).Length == 0
                || InputFormat.formatString(tb_triGia.Text).Length == 0)
            {
                MessageBox.Show("Cần cung cấp đầy đủ thông tin.", "Lỗi");
            }
            else if (!InputFormat.validDateTime(datetime_NgayNhap.Value.Date) || !InputFormat.validDateTime(datetime_NgayNhap.Value.Date))
            {
                MessageBox.Show("Thời gian không hợp lệ.", "Lỗi");
            }
            else if (!Regulations.EligibleNamXB(int.Parse(tb_namXB.Text)))
            {
                ThamSoBUS thamSo = new ThamSoBUS();
                MessageBox.Show("Chỉ nhận sách xuất bản trong vòng " + thamSo.GetGiaTriThamSo(thamSo.KCNamXB) + " năm.", "Lỗi");
            }
            else if (comboBox_theLoai.Text.Length == 0)
            {
                MessageBox.Show("Thiếu thông tin thể loại sách.", "Lỗi");
            }
            else if (listBox_tacGia.Items.Count == 0)
            {
                MessageBox.Show("Thiếu thông tin tác giả.", "Lỗi");
            }
            else
            {
                string _ngayNhap = datetime_NgayNhap.Value.Date.ToString();
                string _tenSach = InputFormat.formatString(tb_tenSach.Text);
                string _theLoai = comboBox_theLoai.Text;
                string _nhaXB = comboBox_NXB.Text;
                int _namXB = int.Parse(InputFormat.formatString(tb_namXB.Text));
                int _soLuong = int.Parse(InputFormat.formatString(tb_soLuong.Text));
                int _donGia = int.Parse(InputFormat.formatString(tb_triGia.Text));

                List<string> listTacGia = new List<string>();
                foreach (string item in listBox_tacGia.Items)
                {
                    listTacGia.Add(item);
                    tacGiaBUS.InsertTacGia(item);
                }

                dausachBUS.InsertDauSach(_tenSach, _theLoai, listTacGia);
                sachBUS.InsertSach(_tenSach, _nhaXB, _namXB, _soLuong, _donGia);
                phieuNhapBUS.InsertPhieuNhapSach(_ngayNhap, _donGia);
                cTPhieuNhapBUS.InsertCTPhieuNhapSach(phieuNhapBUS.GetPhieuNhapSach(_ngayNhap), sachBUS.GetSach(_tenSach, _nhaXB, _namXB), _soLuong, _donGia);
                cuonsachBUS.InsertCuonSach(_tenSach, _nhaXB, _namXB, _soLuong);

                mainForm.LoadDataGrid_DSSach();
                MessageBox.Show("Nhập thành công!");
                LamMoi();
            }
        }
        private void button_themTacGia_Click(object sender, EventArgs e)
        {
            ThemTacGia();
        }
        private void button_xoaTacGia_Click(object sender, EventArgs e)
        {
            listBox_tacGia.Items.Remove(listBox_tacGia.SelectedItem);
        }
        private void LamMoi()
        {
            tb_namXB.Text = "";
            tb_soLuong.Text = "1";
            tb_tenSach.Text = "";
            tb_triGia.Text = "";
            comboBox_NXB.Text = "";
            comboBox_tacGia.Text = "";
            comboBox_theLoai.Text = "";
            listBox_tacGia.Items.Clear();
        }
        private void ThemTacGia()
        {
            string temp = comboBox_tacGia.Text;
            if (temp.Length != 0)
            {
                foreach (string temp1 in listBox_tacGia.Items)
                {
                    if (temp.Equals(temp1))
                    {
                        return;
                    }
                }
                listBox_tacGia.Items.Add(temp);
                comboBox_tacGia.Text = "";
            }
            else MessageBox.Show("Chưa nhập tác giả.");
        }

        private void tb_namXB_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        private void tb_soLuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        private void tb_triGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        private void button_huy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void comboBox_tacGia_SelectedIndexChanged(object sender, EventArgs e)
        {
            string tenThem = comboBox_tacGia.Text;
            if (tenThem != "" && !listBox_tacGia.Items.Contains(tenThem))
                listBox_tacGia.Items.Add(tenThem);
        }
        private void comboBox_tacGia_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                ThemTacGia();
            }
        }
    }
}
